// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Sliding Floor Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 59 : TYPE_SLIDINGFLOOR

// Function declarations
reserve function SlidingFloor_DebugDraw
reserve function SlidingFloor_DebugSpawn

// Static Values

// Tables

function SlidingFloor_DebugDraw
	DrawSprite(0)
end function


function SlidingFloor_DebugSpawn
	CreateTempObject(TypeName[Sliding Floor], 0, object.xpos, object.ypos)
	if object.direction == FLIP_NONE
		object[tempObjectPos].xvel = 0x80000
	else
		object[tempObjectPos].xvel = -0x80000
	end if
	object[tempObjectPos].value1 = object.xpos
end function


event ObjectMain
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -64, -12, 64, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.priority = PRIORITY_ACTIVE
				object.state++
			end if
		next
		break
	case 1
		object.value0++
		if object.value0 == 16
			object.value0 = 0
			object.state++
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -64, -12, 64, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].xpos += object.xvel
			end if
		next
		object.xpos += object.xvel
		break
	case 2
		object.value0++
		if object.value0 == 360
			object.value0 = 0
			object.state++
		end if
		if object.value0 < 3
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_PLATFORM, object.entityPos, -72, -12, 72, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			next
		else
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_PLATFORM, object.entityPos, -64, -12, 64, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			next
		end if
		break
	case 3
		object.value0++
		if object.value0 == 16
			object.value0 = 0
			object.state = 0
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -64, -12, 64, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].xpos -= object.xvel
			end if
		next
		object.xpos -= object.xvel
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.xpos
		object.xpos = object.value1
		if object.outOfBounds == 1
			object.value0 = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
			object.state = 0
		else
			object.xpos = temp0
		end if
	end if
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-64, -12, 128, 24, 383, 115)
	foreach (TypeName[Sliding Floor], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].propertyValue &= 1
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].xvel = 0x80000
		else
			object[arrayPos0].xvel = -0x80000
		end if
		object[arrayPos0].value1 = object[arrayPos0].xpos
	next
	SetTableValue(TypeName[Sliding Floor], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SlidingFloor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SlidingFloor_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
